<template>
  <el-dialog title="新增角色" :visible="showDialog" @close="close">
    <el-form ref="addRoleRef" :model="addRoleForm" :rules="addRules" label-width="120px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model.trim="addRoleForm.name" />
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model.trim="addRoleForm.description" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="close">取消</el-button>
        <el-button size="small" type="primary" @click="addOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addRole } from '@/api/setting'
export default {
  name: 'AddRole',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      addRoleForm: {
        name: '', // 角色名称
        description: '' // 角色描述
      },
      addRules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {

  },
  created() {

  },
  methods: {
    // 退出
    close() {
      this.addRoleForm = {
        name: '',
        description: ''
      }
      this.$emit('update:showDialog', false) // 关闭弹窗
      this.$refs.addRoleRef.resetFields() // 清空数据
    },
    // 添加角色
    async addOK() {
      await this.$refs.addRoleRef.validate(async(valid) => {
        if (valid) {
          await addRole(this.addRoleForm)
          this.$emit('addSuccess') // 刷新数据
          this.close()
          this.$message.success('新增角色成功')
        } else {
          return this.$message.error('新增角色失败')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
